package com.ruoyi.basic.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.basic.domain.Course;
import com.ruoyi.basic.domain.CourseResult;
import com.ruoyi.basic.domain.DeptResult;
import com.ruoyi.basic.mapper.CourseMapper;
import com.ruoyi.basic.service.CourseService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("courseService")
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Value("${token.secret}")
    private String secret;

    @Override
    public PageInfo find(Integer pageNum,Integer pageSize, Integer classCode,String name, String tno, String tname) {
        //分页插件
        PageHelper.startPage(pageNum,pageSize);
        //获取用戶名
        String username = SecurityUtils.getUsername();
        Course course = new Course();
        course.setClassCode(classCode);
        course.setName(name);
        course.setTno(tno);
        course.setTname(tname);
        course.setUserName(username);
        List<Course> courses = courseMapper.find(course);
        //分页信息
        PageInfo pageInfo = new PageInfo(courses);
        return pageInfo;
    }

//    @Override
    //获取课程对应学生信息
    public PageInfo findStudent(Integer classCode) {
        //分页插件
//        PageHelper.startPage(pageNum,pageSize);
        List<Course> courses = courseMapper.findStudent(classCode);
        //分页信息
        PageInfo pageInfo = new PageInfo(courses);
        return pageInfo;
    }

    @Override
//    获取所有用户信息
    public PageInfo findAllStudent(Integer roleId) {
        List<CourseResult> courseResults = null;
        if(roleId  == 2 || roleId ==3){
           courseResults = courseMapper.findAllStudent(roleId);
            //分页信息
        }
        return new PageInfo(courseResults);
    }



//    @Override
    @Transactional
    //    修改课程信息
    public PageInfo updataCourse(Course course) {
        Integer classCode = course.getClassCode();
        String tno = course.getTno();
        String tname = course.getTname();
        //更新kaoqin_Class
        courseMapper.updateKaoqinClass(classCode,tno,tname);
        //更新kaoqin_list
        courseMapper.updateKaoqinList(classCode,tno,tname);
        return null;
    }

    @Override
//    获取所有用户信息
    public List<DeptResult> findDept() {
        List<DeptResult> deptResults = null;
//        if(roleId  == 2 || roleId ==3){
//            courseResults = courseMapper.findAllStudent(roleId);
//            //分页信息
//        }
        deptResults = courseMapper.findDept();
        return deptResults;
    }
}
